//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "InterestRate.h"
using namespace Cephei::QL;
#include <gen/QL/Times/DayCounter.h>
using namespace Cephei::QL::Times;
#undef HANDLE
#undef ABSTRACT
#define STRUCT
Cephei::QL::CInterestRate::CInterestRate (Double r, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phInterestRate = NULL;
#endif
        QuantLib::Rate _r = (QuantLib::Rate)ValueHelper::Convert (r);
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        _ppInterestRate = new boost::shared_ptr<QuantLib::InterestRate> (new QuantLib::InterestRate ( _r,  _dc,  _comp,  _freq ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::CInterestRate::CInterestRate () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phInterestRate = NULL;
#endif
        _ppInterestRate = new boost::shared_ptr<QuantLib::InterestRate> (new QuantLib::InterestRate ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CInterestRate::CInterestRate (boost::shared_ptr<QuantLib::InterestRate>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterestRate = NULL;
#endif
	_ppInterestRate = &childNative;
    
}
Cephei::QL::CInterestRate::CInterestRate (QuantLib::InterestRate& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phInterestRate = NULL;
#endif
	_ppInterestRate = new boost::shared_ptr<QuantLib::InterestRate> (&childNative);
    
    _InterestRateOwner = owner;
    
}

Cephei::QL::CInterestRate::CInterestRate (CInterestRate^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterestRate = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppInterestRate = new boost::shared_ptr<QuantLib::InterestRate> (copy->GetShared());
        
    }
}
Cephei::QL::CInterestRate::CInterestRate (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterestRate = NULL;
#endif
	if (!t->IsSubclassOf(CInterestRate::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CInterestRate::CInterestRate (QuantLib::Handle<QuantLib::InterestRate>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInterestRate = &childNative;
	_ppInterestRate = &static_cast<boost::shared_ptr<QuantLib::InterestRate>>(childNative.currentLink());
    
    _InterestRateOwner = owner;
}
Cephei::QL::CInterestRate::CInterestRate (QuantLib::Handle<QuantLib::InterestRate> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phInterestRate = &childNative;
	_ppInterestRate = &static_cast<boost::shared_ptr<QuantLib::InterestRate>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CInterestRate::CInterestRate (QuantLib::InterestRate childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phInterestRate = NULL;
#endif
	_ppInterestRate = new boost::shared_ptr<QuantLib::InterestRate> (new QuantLib::InterestRate (childNative));
    
}
#endif

Cephei::QL::CInterestRate::~CInterestRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppInterestRate != NULL)
    {
	    delete _ppInterestRate;
        _ppInterestRate = NULL;
    }
}
Cephei::QL::CInterestRate::!CInterestRate ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppInterestRate != NULL)
    {
	    delete _ppInterestRate;
    }
}
QuantLib::InterestRate& Cephei::QL::CInterestRate::GetReference ()
{
    if (_ppInterestRate == NULL) throw gcnew NativeNullException ();
	return **_ppInterestRate;
}
boost::shared_ptr<QuantLib::InterestRate>& Cephei::QL::CInterestRate::GetShared ()
{
    if (_ppInterestRate == NULL) throw gcnew NativeNullException ();
	return *_ppInterestRate;
}
QuantLib::InterestRate* Cephei::QL::CInterestRate::GetPointer ()
{
    if (_ppInterestRate == NULL) throw gcnew NativeNullException ();
	return &**_ppInterestRate;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::InterestRate>& Cephei::QL::CInterestRate::GetHandle ()
{
	if (_phInterestRate == NULL)
	{
		_phInterestRate = new Handle<QuantLib::InterestRate> (*_ppInterestRate);
	}
	return *_phInterestRate;
}
#endif
bool Cephei::QL::CInterestRate::HasNative () 
{
	return (_ppInterestRate != NULL);
}

Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate::EquivalentRate (Cephei::QL::Times::IDayCounter^ resultDC, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, DateTime d1, DateTime d2, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refEnd)
{
    CDayCounter^ _CresultDC;
    try
    {
        _CresultDC = safe_cast<CDayCounter^> (resultDC);
        _CresultDC->Lock();
        QuantLib::DayCounter& _resultDC = static_cast<QuantLib::DayCounter&> (_CresultDC->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Date _d1 = (QuantLib::Date)ValueHelper::Convert (d1);
        QuantLib::Date _d2 = (QuantLib::Date)ValueHelper::Convert (d2);
        QuantLib::Date _refStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refStart) ? (QuantLib::Date)ValueHelper::Convert (refStart->Value) : QuantLib::Date()); //9a
        QuantLib::Date _refEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refEnd) ? (QuantLib::Date)ValueHelper::Convert (refEnd->Value) : QuantLib::Date()); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppInterestRate)->equivalentRate ( _resultDC,  _comp,  _freq,  _d1,  _d2,  _refStart,  _refEnd );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CresultDC != nullptr) _CresultDC->Unlock();
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate::EquivalentRate (QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Double t)
{
    try
    {
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppInterestRate)->equivalentRate ( _comp,  _freq,  _t );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate::ImpliedRate (Double compound, Cephei::QL::Times::IDayCounter^ resultDC, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, DateTime d1, DateTime d2, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refEnd)
{
    CDayCounter^ _CresultDC;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _compound = (QuantLib::Real)ValueHelper::Convert (compound);
        _CresultDC = safe_cast<CDayCounter^> (resultDC);
        _CresultDC->Lock();
        QuantLib::DayCounter& _resultDC = static_cast<QuantLib::DayCounter&> (_CresultDC->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Date _d1 = (QuantLib::Date)ValueHelper::Convert (d1);
        QuantLib::Date _d2 = (QuantLib::Date)ValueHelper::Convert (d2);
        QuantLib::Date _refStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refStart) ? (QuantLib::Date)ValueHelper::Convert (refStart->Value) : QuantLib::Date()); //9a
        QuantLib::Date _refEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refEnd) ? (QuantLib::Date)ValueHelper::Convert (refEnd->Value) : QuantLib::Date()); //9a
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppInterestRate)->impliedRate ( _compound,  _resultDC,  _comp,  _freq,  _d1,  _d2,  _refStart,  _refEnd );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CresultDC != nullptr) _CresultDC->Unlock();
    }
}
Double Cephei::QL::CInterestRate::CompoundFactor (DateTime d1, DateTime d2, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refEnd)
{
    try
    {
        QuantLib::Date _d1 = (QuantLib::Date)ValueHelper::Convert (d1);
        QuantLib::Date _d2 = (QuantLib::Date)ValueHelper::Convert (d2);
        QuantLib::Date _refStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refStart) ? (QuantLib::Date)ValueHelper::Convert (refStart->Value) : QuantLib::Date()); //9a
        QuantLib::Date _refEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refEnd) ? (QuantLib::Date)ValueHelper::Convert (refEnd->Value) : QuantLib::Date()); //9a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterestRate)->compoundFactor ( _d1,  _d2,  _refStart,  _refEnd );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInterestRate::CompoundFactor (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppInterestRate)->compoundFactor ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::CompoundingEnum Cephei::QL::CInterestRate::Compounding::get ()
{
    try
    {
    	QuantLib::Compounding _rv = (QuantLib::Compounding)(*_ppInterestRate)->compounding ( );   
        QL::CompoundingEnum _nrv = (QL::CompoundingEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::CInterestRate::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter& _rv = (QuantLib::DayCounter&)(*_ppInterestRate)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = gcnew Cephei::QL::Times::CDayCounter (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInterestRate::DiscountFactor (DateTime d1, DateTime d2, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refStart, Microsoft::FSharp::Core::FSharpOption<DateTime>^ refEnd)
{
    try
    {
        QuantLib::Date _d1 = (QuantLib::Date)ValueHelper::Convert (d1);
        QuantLib::Date _d2 = (QuantLib::Date)ValueHelper::Convert (d2);
        QuantLib::Date _refStart = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refStart) ? (QuantLib::Date)ValueHelper::Convert (refStart->Value) : QuantLib::Date()); //9a
        QuantLib::Date _refEnd = 
            (Microsoft::FSharp::Core::FSharpOption<DateTime>::IsSome::get (refEnd) ? (QuantLib::Date)ValueHelper::Convert (refEnd->Value) : QuantLib::Date()); //9a
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppInterestRate)->discountFactor ( _d1,  _d2,  _refStart,  _refEnd );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInterestRate::DiscountFactor (Double t)
{
    try
    {
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::DiscountFactor _rv = (QuantLib::DiscountFactor)(*_ppInterestRate)->discountFactor ( _t );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate::ImpliedRate (Double compound, Cephei::QL::Times::IDayCounter^ resultDC, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq, Double t)
{
    CDayCounter^ _CresultDC;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _compound = (QuantLib::Real)ValueHelper::Convert (compound);
        _CresultDC = safe_cast<CDayCounter^> (resultDC);
        _CresultDC->Lock();
        QuantLib::DayCounter& _resultDC = static_cast<QuantLib::DayCounter&> (_CresultDC->GetReference ()); 
        QuantLib::Compounding _comp = (QuantLib::Compounding)comp ;
        QuantLib::Frequency _freq = (QuantLib::Frequency)freq ;
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
    	QuantLib::InterestRate _rv = (QuantLib::InterestRate)(*_ppInterestRate)->impliedRate ( _compound,  _resultDC,  _comp,  _freq,  _t );   
        Cephei::QL::CInterestRate^ _nrv = gcnew Cephei::QL::CInterestRate (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_CresultDC != nullptr) _CresultDC->Unlock();
    }
}
QL::Times::FrequencyEnum Cephei::QL::CInterestRate::Frequency::get ()
{
    try
    {
    	QuantLib::Frequency _rv = (QuantLib::Frequency)(*_ppInterestRate)->frequency ( );   
        QL::Times::FrequencyEnum _nrv = (QL::Times::FrequencyEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::CInterestRate::Rate::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppInterestRate)->rate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CInterestRateVector::CInterestRateVector ()
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
}
Cephei::QL::CInterestRateVector::~CInterestRateVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
	_pVValue = NULL;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
	_pVBoost = NULL;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
	_pVHand = NULL;
#endif
}
Cephei::QL::CInterestRateVector::!CInterestRateVector ()
{
#ifdef STRUCT
	if (_pVValue != NULL) delete _pVValue;
#endif   
	if (_pVBoost != NULL) delete _pVBoost;
#ifdef HANDLE
	if (_pVHand != NULL) delete _pVHand;
#endif
}
#ifdef STRUCT
Cephei::QL::CInterestRateVector::CInterestRateVector (std::vector<QuantLib::InterestRate>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = &vec;
	_owner = owner;
}
Cephei::QL::CInterestRateVector::CInterestRateVector (std::vector<QuantLib::InterestRate> vec)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	_pVValue = new std::vector<QuantLib::InterestRate> (vec);
}
#endif
Cephei::QL::CInterestRateVector::CInterestRateVector (std::vector<boost::shared_ptr<QuantLib::InterestRate>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVBoost = &vec;
	_owner = owner;
}
#ifdef HANDLE
Cephei::QL::CInterestRateVector::CInterestRateVector (std::vector<QuantLib::Handle<QuantLib::InterestRate>>& vec, Object^ owner)
{
#ifdef STRUCT
	_pVValue = NULL;
#endif
	_pVBoost = NULL;
#ifdef HANDLE
	_pVHand = NULL;
#endif
	 _pVHand = &vec;
	_owner = owner;
}
#endif

int Cephei::QL::CInterestRateVector::Count::get ()
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
		return _pVValue->size();
	}
#endif    
	if (_pVBoost != NULL)
	{
		return _pVBoost->size ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		return _pVHand->size ();
	}
#endif
	return 0;		// should never happen
}
Cephei::QL::IInterestRate^ Cephei::QL::CInterestRateVector::default::get (int index) 
{
#ifdef STRUCT
	if (_pVValue != NULL) 
	{
    	if ((size_t)index >= _pVValue->size()) throw gcnew RangeException ();
		QuantLib::InterestRate& native = (*_pVValue)[index];
		return gcnew CInterestRate (native, this);
	}
#endif    
	if (_pVBoost != NULL)
	{
    	if ((size_t)index >= _pVBoost->size()) throw gcnew RangeException ();
		QuantLib::InterestRate& native = *(*_pVBoost)[index];
		return gcnew CInterestRate (native, this);
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
    	if ((size_t)index >= _pVHand->size()) throw gcnew RangeException ();
		QuantLib::InterestRate& native = *((*_pVHand)[index].currentLink ());
		return gcnew CInterestRate (native, this);
	}
#endif
	return nullptr;		// can never be the case
}
void Cephei::QL::CInterestRateVector::default::set (int index, Cephei::QL::IInterestRate^ value) 
{
#ifdef STRUCT
	if (_pVValue != NULL)
	{
		if (_pVValue->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVValue->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVValue->push_back (dynamic_cast<CInterestRate^>(value)->GetReference ());
		}
        else
        {
            QuantLib::InterestRate& c = (*_pVValue)[index];
            c = dynamic_cast<CInterestRate^>(value)->GetReference ();
        }
	}
#endif   
	if (_pVBoost != NULL)
	{
		if (_pVBoost->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVBoost->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVBoost->push_back (dynamic_cast<CInterestRate^>(value)->GetShared ());
		}
		boost::shared_ptr<QuantLib::InterestRate>& c = (*_pVBoost)[index];
		c = dynamic_cast<CInterestRate^>(value)->GetShared ();
	}
#ifdef HANDLE
	if (_pVHand != NULL)
	{
		if (_pVHand->size() <= (size_t)index)
		{
			// can't happen because vectors are filled from List<> sequentially
			if (_pVHand->size() < (size_t)index)
			{
				throw gcnew RangeException ();
			}
			_pVHand->push_back (QuantLib::Handle<QuantLib::InterestRate> ());
		}
		QuantLib::Handle<QuantLib::InterestRate>& c = (*_pVHand)[index];
		boost::shared_ptr<QuantLib::InterestRate>& b = static_cast<boost::shared_ptr<QuantLib::InterestRate>>(c.currentLink());
		b = dynamic_cast<CInterestRate^>(value)->GetShared ();
	}
#endif
}
Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^ Cephei::QL::CInterestRateVector::CItem::get (int index) 
{
	return CellBuilder::Instance->Create<IInterestRate^> (dynamic_cast<IInterestRate^>(default[index]));
}

void Cephei::QL::CInterestRateVector::CItem::set (int index, Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^ value) 
{
	default[index] = safe_cast<IInterestRate^>(value->Value);
}
#ifdef STRUCT
std::vector<QuantLib::InterestRate>& Cephei::QL::CInterestRateVector::GetReference ()
{
	 return *_pVValue;
}
#endif

std::vector<boost::shared_ptr<QuantLib::InterestRate>>& Cephei::QL::CInterestRateVector::GetShared ()
{
	return *_pVBoost;
}

#ifdef HANDLE
std::vector<QuantLib::Handle<QuantLib::InterestRate>>& Cephei::QL::CInterestRateVector::GetHandle ()
{
	return *_pVHand;
}
#endif
bool Cephei::QL::CInterestRateVector::PrepareFeature (NativeFeature feature)
{
	switch (feature)
	{
#ifdef STRUCT
		case NativeFeature::Value :
			if (_pVValue == NULL)
			{
				_pVValue = new std::vector<QuantLib::InterestRate> ();
				return true;
			}
			break;
#endif
        case NativeFeature::shared_ptr :
			if (_pVBoost == NULL)
			{
				_pVBoost = new std::vector<boost::shared_ptr<QuantLib::InterestRate>> ();
				return true;
			}
			break;
#ifdef HANDLE
        case NativeFeature::Handle :
            if (_pVHand == NULL)
            {
                _pVHand = new std::vector<QuantLib::Handle<QuantLib::InterestRate>> ();
                return true;
            }
            break;
#endif
	}
	return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate_Factory::Create (Double r, Cephei::QL::Times::IDayCounter^ dc, QL::CompoundingEnum comp, QL::Times::FrequencyEnum freq)
{
    return gcnew CInterestRate ( r,  dc,  comp,  freq);
}
Cephei::QL::IInterestRate^ Cephei::QL::CInterestRate_Factory::Create ()
{
    return gcnew CInterestRate ();
}
/* <summary>Create a single dimension array of InterestRate that can be passed into other function </summary> */
IVector<Cephei::QL::IInterestRate^>^ Cephei::QL::CInterestRate_Factory::CreateVector ()
 {
	 return gcnew CoVector<IInterestRate^> (gcnew CInterestRateVector ());
 }
IVector<Cephei::QL::IInterestRate^>^ Cephei::QL::CInterestRate_Factory::CreateVector (System::Collections::Generic::IEnumerable<IInterestRate^>^ source)
 {
	 return gcnew CoVector<IInterestRate^> (source, gcnew CInterestRateVector ());
 }
/* <summary>Create a single dimension array of InterestRate with change events</summary> */
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^>^>^ Cephei::QL::CInterestRate_Factory::CreateCellVector ()
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^> (gcnew CInterestRateVector ()));
 }
Cephei::Generic::ICell<IVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^>^>^ Cephei::QL::CInterestRate_Factory::CreateCellVector (System::Collections::Generic::IEnumerable<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^>^ source)
 {
	 return CellBuilder::Instance->Create<IVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^>^> (gcnew CoCellVector<Cephei::Generic::ICell<Cephei::QL::IInterestRate^>^> (source, gcnew CInterestRateVector ()));
 }

